package com.ljy.my_study.java.juc.exchanger;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Exchanger;

/** 
* @author James
* @date 2018年9月10日 
*/
public class TestMain {
	public static void main(String[] args) {
		final Exchanger<List<Integer>> exchanger=new Exchanger<List<Integer>>();
		new Thread() {
			public void run() {
				List<Integer> l=new ArrayList<>(2);
				l.add(1);
				l.add(2);
				try {
					l=exchanger.exchange(l);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println("thread1->list:"+l);
			}
		}.start();
		new Thread() {
			public void run() {
				List<Integer> l=new ArrayList<>(2);
				l.add(4);
				l.add(5);
				try {
					l=exchanger.exchange(l);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println("thread2->list:"+l);
			}
		}.start();
	}
}
